﻿
document.write("<script language='javascript' src='/resources/js/views/defineTable.js'></script>");
document.write("<script language='javascript' src='/resources/js/views/defineInit.js'></script>");
var action = "/department/list.do";

var page = 1;
var pageSize = 30;

function query(curPage, pageSize) {
	
	curPage = parseInt(curPage);
	pageSize = parseInt(pageSize);
	var searchname = $('#searchname').val();
	var type=null;
	var level=null;
	var status=null;
	
	$.get(action, {"name": searchname, "type": type, "level": level, "status": status, "page": curPage, "pageSize": pageSize}, function (data) {
			
			var departmentTypes = data["departmentTypes"];
			var departmentes = data["departmentes"];
			var areaList = data["areaList"];
			var total = data["total"];
			var totalPage = Math.ceil(total / pageSize);
			var start = (curPage-1) * pageSize;
		    
			data = data["rows"];
			
			for(var i=0; i<data.length; i++){
				//获取分类名称
				for(var j=0; j<departmentTypes.length; j++){
					if(data[i]["type"] === departmentTypes[j]["id"]){
						data[i]["typename"] = departmentTypes[j]["name"];
						break;
					}
				}
				//获取地域的名称
				for(var j=0; j<areaList.length; j++){
					if(data[i]["area"] === areaList[j]["id"]){
						data[i]["aname"] = areaList[j]["name"];
						break;
					}
				}
				//获取父类名称
				if(data[i]["pid"] == 0){
					data[i]["pname"] = "陕西省";
				}else{
					for(var j=0; j<departmentes.length; j++){
						if(data[i]["pid"] === departmentes[j]["id"]){
							data[i]["pname"] = departmentes[j]["name"];
							break;
						}
					}
				}
			}
			
            $('#tt').datagrid( {
                width : '100%',
                height : 'auto',
                fitColumns: true,
                columns: [defineTableDepartShowColumns],
                singleSelect:true,// 是否单选
                striped:true,
                pageNumber: curPage,//显示数据列用
                pageSize: pageSize,
                pageList: [pageSize, 50, 100],
                rownumbers: false,
                pagination: true,//打开翻页功能
                data: data,
                toolbar: defineInitCreateToolBar(true,true,true,false)
            });

            var p = $('#tt').datagrid('getPager');
            $(p).pagination({
                total: total,
                pageList: [pageSize, 50, 100],
                pageSize: pageSize,
                pageNumber: curPage,
                beforePageText: '第',
                afterPageText: '页    共 ' + totalPage + ' 页',
                displayMsg: '当前显示 ' + (start + 1) + '-' + (start + data.length) + ' 条记录 共 ' + total + ' 条记录',
                onSelectPage: function (curPage, pageSize) {
                	query(curPage, pageSize);
                },
                onChangePageSize: function (ps) {
                    pageSize = ps;
                }
            });

			try{window.parent.unmask();}catch(err){}
			
	}, "json");
}


$(document).ready(function () {

    $.ajaxSetup({cache: false});
    
	query(page, pageSize);
	
	$('#fmAdd').form( {
		success : function(data) {
		    CRUD.queryList();
		    try{
			    var result = eval('(' + data + ')');
			    if(result == true){
			    	$('#dialogAdd').dialog('close');
				 } else {
			        $.messager.alert('提示', '保存失败');
				 }
			}catch(err){
			}
		}
	});

    CRUD.queryList();
    InitForm();

    // 确定添加
    $('#a_AddOK').click(function () {
        if (!document.getElementById("code").value) {
            $.messager.alert('提示', '部门编码不能为空!');
            return;
        }
	    if (!document.getElementById("name").value) {
	    	$.messager.alert('提示', '部门名称不能为空!');
	    	return;
	    }
	    if (!document.getElementById("sname").value) {
	    	$.messager.alert('提示', '部门简称不能为空!');
	    	return;
	    }
        if (!$('#type').combobox("getValue")) {
        	$.messager.alert('提示', '部门类型不能为空!');
        	return;
        }
        if (!$('#pid').combobox("getValue")) {
        	$.messager.alert('提示', '上级部门不能为空!');
        	return;
        }
        if (!$('#area').combobox("getValue")) {
        	$.messager.alert('提示', '所属领域不能为空!');
        	return;
        }
        if (!$('#level').combobox("getValue")) {
        	$.messager.alert('提示', '部门级别不能为空!');
        	return;
        }
	    if (!document.getElementById("charge").value) {
	    	$.messager.alert('提示', '部门负责人不能为空!');
	    	return;
	    }
        
        $('#fmAdd').submit();
        CRUD.queryList();
    });

    // 取消
    $('#a_AddCancle').click(function () {
    	var textarea = $('#idScales');
    	if (textarea) {
    		textarea.remove();
    	}
        $('#fmAdd').form('clear');
        $('#dialogAdd').dialog('close');
        $('#tt').datagrid("clearSelections");
    });
});

var CRUD = {
    queryList: function () {
		try{window.parent.mask();}catch(err){}
	   	query(page, pageSize);
    },

    Add: function () {
    	$('#fmAdd').form('clear');
    	var textarea = $('#idScales');
    	if (textarea) {
    		textarea.remove();
    	}
        $('#dialogAdd').dialog('open');
        $('#name').removeAttr("disabled");
        $('#sname').removeAttr("disabled");
        $('#code').removeAttr("disabled");
        $('#type').combobox({
            disabled:false
        });
        $('#pid').combobox({
            disabled:false
        });
        $('#level').combobox({
            disabled:false
        });
        $('#area').combobox({
            disabled:false
        });
        $('#tt').datagrid("clearSelections");
        $('#dialogAdd').dialog("setTitle", "添加部门信息");
    },
    
    Del: function () {
        var r = $('#tt').datagrid('getSelected');
        if (r) {
            $.messager.confirm('删除提示', '你确认删除这一条数据吗？', function (d) {
                if (d) {
                    $.get(getContextPath() + "/department/delete.do?id=" + r.id, function (data) {
                        if (data == 'true') {
                            $.messager.show(
                                {
                                    title: '系统提示',
                                    msg: '删除成功！',
                                    timeout: 1500
                                });
                            $('#tt').datagrid("clearSelections");
                            CRUD.queryList();
                        } else {
                            $.messager.alert('错误', '删除失败！');
                        }
                    });
                }
            });
        } else {
            $.messager.alert('提示', '请选择一行数据!');
        }
    },
    Update: function () {
        var r = $('#tt').datagrid('getSelected');
        if (r == null) {
            $.messager.alert('提示', '请选择一行数据');
            return;
        }
        $('#dialogAdd').dialog('open');
        $('#fmAdd').form('load', r);
        $('#dialogAdd').dialog("setTitle", "修改部门信息");
    }
};

function modify(id,code,name,sname,type,level,pid,area,charge,phone,author,email,address) {

	pid = setEmptyForm(pid);
	phone = setEmptyForm(phone);
    author = setEmptyForm(author);
	email = setEmptyForm(email);
	address = setEmptyForm(address);

    $('#dialogAdd').dialog('open');
    $('#name').attr("disabled",'disabled');
    $('#sname').attr("disabled",'disabled');
    $('#code').attr("disabled",'disabled');
    $('#type').combobox({
        disabled:true
    });
    $('#pid').combobox({
        disabled:true
    });
    $('#level').combobox({
        disabled:true
    });
    $('#area').combobox({
        disabled:true
    });
    //$('#type').attr("readonly",'readonly');
    //$('#pid').attr("readonly",'readonly');
    //$('#level').attr("readonly",'readonly');
    //$('#area').attr("readonly",'readonly');
    $('#fmAdd').form('load',{
        id: id,
        code: code,
        name: name,
        sname: sname,
        type: type,
        level: level,
        pid: pid,
        area: area,
        charge: charge,
        author: author,
        phone: phone,
        email: email,
        address: address
    	});
    $('#dialogAdd').dialog("setTitle", "修改部门信息");
}
	
function del(id) {
	$.messager.confirm('删除该部门信息', '你确认删除该部门信息吗?', function (d) {
		if (d) {
			$.get(getContextPath() + "/department/delete.do?id=" + id, function (data) {
				if (data == 'true') {
					$.messager.show({
						title: '系统提示',
						msg: '删除成功！',
						timeout: 1500
					});
					$('#tt').datagrid("clearSelections");
					CRUD.queryList();
				} else {
					$.messager.alert('错误', '删除失败！');
				}
			});
		}
	});
}

function InitForm() {
    var width = 500;
    var height = window.screen.height;
    if (height > 500) {
        height = 430;
    }
    $('#dialogAdd').dialog({
        closed: true,
        title: "添加部门信息",
        width: width,
        height: height
    });
}

function setEmptyForm(value) {
	if(typeof(value) == "undefined"||value == "null"){
		return "";
	}else{
		return value;
	}
}

